using System.Collections.Generic;
using System.Linq;
using EPiServer.Security;

namespace OpenWaves.ActivityFeed.EPiServer
{
    public static class AccessControlListExtensions
    {
        public static IEnumerable<AccessControlEntry> GetEntries(this AccessControlList list)
        {
            return list.Keys.Cast<string>().Select(k => list[k]).ToList();
        }

        public static IEnumerable<AccessControlEntry> ForRoles(this IEnumerable<AccessControlEntry> list)
        {
            return list.Where(e => e.EntityType == SecurityEntityType.Role);
        }

        public static IEnumerable<AccessControlEntry> ForUsers(this IEnumerable<AccessControlEntry> list)
        {
            return list.Where(e => e.EntityType == SecurityEntityType.User);
        }

        public static IEnumerable<AccessControlEntry> WithReadAccess(this IEnumerable<AccessControlEntry> list)
        {
            return list.Where(e => (e.Access & AccessLevel.Read) == AccessLevel.Read);
        }
    }
}